package com.baicaixiaozhan.springsecurityswaggerdemo.service.impl;

import com.baicaixiaozhan.springsecurityswaggerdemo.dao.ApplicationUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * DESC:
 *
 * @author : baicaixiaozhan
 * @since : 2020-11-05
 */
@Service
public class ApplicationUserServiceImpl implements UserDetailsService {

    private final ApplicationUserDao applicationUserDao;
    @Autowired
    public ApplicationUserServiceImpl(@Qualifier("fake") ApplicationUserDao applicationUserDao) {
        this.applicationUserDao = applicationUserDao;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return applicationUserDao
                .selectApplicationUserByUsername(username)
                .orElseThrow(() ->
                        new UsernameNotFoundException(String.format("username %s not found", username)));
    }
}
